//
//  XMN_GroupDetailViewController.m
//  B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_GroupDetailViewController.h"
#import "XMN_GroupDetailCell.h"

@interface XMN_GroupDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSDictionary *dataDic;
@property(nonatomic, strong)XMN_ProgressHUD *HUD;
@end

@implementation XMN_GroupDetailViewController

-(void)loadView{
    [super loadView];
    
    _dataDic = [NSDictionary dictionary];
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    [_tableView NightWithType:UIViewColorType2];
    [self.view addSubview:_tableView];
    
    [self getDataByURLString];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _group.title;
    
    _HUD = [[XMN_ProgressHUD alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
    _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _HUD.XMN_label.text = @"Waiting...";
    [self.view addSubview:_HUD];
    // Do any additional setup after loading the view.
}

#pragma mark - tableView相关设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    XMN_GroupDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XMN_GroupDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell.nameImageView sd_setImageWithURL:[NSURL URLWithString:[[_dataDic objectForKey:@"userinfo"] objectForKey:@"icon"]]];
    [cell.nameImageView sd_setImageWithURL:[NSURL URLWithString:[[_dataDic objectForKey:@"userinfo"] objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.nameLabel.text = [NSString stringWithFormat:@"组长: %@", [[_dataDic objectForKey:@"userinfo"] objectForKey:@"uname"]];
    cell.typeILView.leftImageView.image = [UIImage imageNamed:@"XMN_type"];
    switch ([[_dataDic objectForKey:@"tags"] count]) {
        case 0:{
            cell.typeILView.rightLabel.text = @"未标注";
            break;
        }
        case 1:{
            cell.typeILView.rightLabel.text = [[_dataDic objectForKey:@"tags"] objectAtIndex:0];
            break;
        }
        case 2:{
            cell.typeILView.rightLabel.text = [NSString stringWithFormat:@"%@/%@", [[_dataDic objectForKey:@"tags"] objectAtIndex:0], [[_dataDic objectForKey:@"tags"] objectAtIndex:1]];
            break;
        }
        default:{
            cell.typeILView.rightLabel.text = [NSString stringWithFormat:@"%@/%@/%@", [[_dataDic objectForKey:@"tags"] objectAtIndex:0], [[_dataDic objectForKey:@"tags"] objectAtIndex:1], [[_dataDic objectForKey:@"tags"] objectAtIndex:2]];
            break;
        }
    }
    cell.contentLabel.text = [NSString stringWithFormat:@"简介: \n\n%@", [_dataDic objectForKey:@"desc"]];
    
    //tableView点击样式
    UIView *cellBackView = [[UIView alloc]initWithFrame:cell.frame];
    cellBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:.2];
    cell.selectedBackgroundView = cellBackView;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Height;
}

#pragma mark - 数组解析
-(void)getDataByURLString{
    [HTTPMethod getDataByString:@"http://api2.pianke.me/group/group_detail" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=DD7B485B-B4B6-4DBA-849E-3A2A14F94824&groupid=%@&version=3.0.5", _group.groupid]] WithDataBlock:^(id data) {
//        NSLog(@"%@", data);
        _dataDic = [[data objectForKey:@"data"] objectForKey:@"groupinfo"];
        [_tableView reloadData];
        [_HUD removeFromSuperview];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
